package com.jcli.novel_t.swing;

import com.jcli.novel_t.exception.FileProcessingException;
import com.jcli.novel_t.TextFileProcessor01;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * 一个思路，不想使用，废弃
 */
public class TextFileProcessorGUI01 {
    private JFrame frame;
    private JTextArea logTextArea;
    private JButton startButton;
    private JFileChooser fileChooser;

    public TextFileProcessorGUI01() {
        frame = new JFrame("文本文件处理工具");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 400);
        frame.setLayout(new BorderLayout());

        logTextArea = new JTextArea();
        logTextArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(logTextArea);
        frame.add(scrollPane, BorderLayout.CENTER);

        startButton = new JButton("开始处理");
        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                handleStartButtonClick();
            }
        });

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(startButton);
        frame.add(buttonPanel, BorderLayout.SOUTH);

        fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    }

    private void handleStartButtonClick() {
        int result = fileChooser.showOpenDialog(frame);
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            String inputFile = selectedFile.getAbsolutePath();
            String outputDirectory = "output_directory";  // 修改为你的输出目录
            String outputFilePath2 = "output_original.txt";  // 修改为你的输出文件路径
            String outputFilePath3 = "output_combined.txt";  // 修改为你的输出文件路径

            // 创建文本文件处理器
            TextFileProcessor01 processor = new TextFileProcessor01(null);  // 需要传入Properties对象

            try {
                processor.processTextFile(inputFile, outputDirectory, outputFilePath2, outputFilePath3);
                log("替换处理已完成，输出文件保存在：" + outputDirectory);
            } catch (FileProcessingException ex) {
                log("文件处理出错: " + ex.getMessage());
            }
        }
    }

    private void log(String message) {
        logTextArea.append(message + "\n");
    }

    public void show() {
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TextFileProcessorGUI01 gui = new TextFileProcessorGUI01();
                gui.show();
            }
        });
    }
}
